#pragma once

#include <iostream>
#include <sstream>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define SEP_LINE "\r\n"
#define SEP_LINE_LEN strlen(SEP_LINE)
// #define fin

#define default_root "./website"
#define default_home "home.html"
#define default_404 "404.html"

namespace zxf
{

    class request
    {
    public:
        request()
        {
        }
        ~request()
        {
        }

        bool parse()
        {

            size_t pos1 = _buffer.find(SEP_LINE);
            std::string tmp = _buffer.substr(0, pos1);
            if (tmp.empty())
            {
                return false;
            }

            // 以空格分割的使用 stringstream / istringstream / ostringstream
            // 更方便
            std::stringstream sin(tmp);
            sin >> _method >> _url >> _http_version;

            // 添加web默认路径
            _path = default_root; // ./website,
            _path += _url;        //./website/a/b/c.html, ./wwwroot/
            // 当_url==/的时候就直接返回home.html(主页)
            if (_path[_path.size() - 1] == '/')
            {
                _path += default_home;
            }

            size_t pos = _path.rfind(".");
            if (pos == std::string::npos)
            {
                _suffix = ".html";
            }
            else
            {
                _suffix = _path.substr(pos);
            }

            // 5. 得到资源的大小
            /// 资源的文件级别的大小
            struct stat st;
            int n = stat(_path.c_str(), &st);
            if (n == 0)
                _size = st.st_size;
            else
            {
                n = stat("./website/home.html", &st);
                if (n == 0)
                {
                    _size = st.st_size;
                }
                else
                {
                    _size = 0;
                }
            }
        }

    public:
        std::string _buffer = "";
        std::string _method = "";
        std::string _url;
        std::string _path = "";
        std::string _http_version = "";
        std::vector<std::string> _str_lines;
        std::string _suffix;
        size_t _size; // 资源的文件级别的大小
    };

    class response
    {
    public:
        std::string _buffer;
    };

} // end namespace zxf
